package com.lb.jwitter.action;


 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
 import java.util.Collection;
import java.util.Date;
import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.lb.jwitter.util.ThoiKhoaBieuPhongNgay;
import com.lb.jwitter.util.ThoiKhoaBieuPhongNgayMonHoc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.opensymphony.xwork2.Action; 
 import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
 import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThuc;
 import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThucService;
 import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuan;
 import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuanService;

import com.lb.jwitter.service.TblQLDTTXADMPhong;
 import com.lb.jwitter.service.TblQLDTTXADMPhongService;

import com.lb.jwitter.service.TblQLDTTXADMTietHoc;
 import com.lb.jwitter.service.TblQLDTTXADMTietHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
 import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class XemDiemSoHocVienSearchAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }
    
    private int view;

    private Collection<TblQLDTTXAThoiKhoaBieuChinhThuc> tblQLDTTXAThoiKhoaBieuChinhThucs; 
    public Collection<TblQLDTTXAThoiKhoaBieuChinhThuc>  getTblQLDTTXAThoiKhoaBieuChinhThucs() {
         return tblQLDTTXAThoiKhoaBieuChinhThucs; 
      } 

    private TblQLDTTXAThoiKhoaBieuChinhThucService tblQLDTTXAThoiKhoaBieuChinhThucService; 

    private List<TblQLDTTXADMNgayTrongTuan> tblQLDTTXADMNgayTrongTuans; 
    public List<TblQLDTTXADMNgayTrongTuan>  getTblQLDTTXADMNgayTrongTuans(){ 
              return tblQLDTTXADMNgayTrongTuans; 
     } 

    private TblQLDTTXADMNgayTrongTuanService tblQLDTTXADMNgayTrongTuanService; 

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 
    
    private TblQLDTTXAHocVien tblQLDTTXAHocVien;
    private String mSHV;

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private List<TblQLDTTXADMTietHoc> tblQLDTTXADMTietHocs; 
    public List<TblQLDTTXADMTietHoc>  getTblQLDTTXADMTietHocs(){ 
              return tblQLDTTXADMTietHocs; 
     } 

    private List<ThoiKhoaBieuPhongNgay> thoiKhoaBieuPhongNgays;
    private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs; 
    public List<TblQLDTTXALopMonHoc>  getTblQLDTTXALopMonHocs(){ 
              return tblQLDTTXALopMonHocs; 
     } 
    
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    private List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens;
    
    private Long hocKyId;

    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 
    
    private int soTinChiTL;
    private int tongSoTinChi;
    private Double diemTBTL;
    
    private TblQLDTTXAHocVienChuongTrinhHocService tblQLDTTXAHocVienChuongTrinhHocService;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_ThoiKhoaBieuChinhThuc", "xem")){ 
        return "noprivilege"; 
    }
    

        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
        
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient"); 

        tblQLDTTXAHocVienChuongTrinhHocService = (TblQLDTTXAHocVienChuongTrinhHocService) context.getBean("tbl_QLDTTXA_HocVien_ChuongTrinhHocClient"); 

        
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        //Get hocVien
        cons.clear();
        if(mSHV != null && !mSHV.equalsIgnoreCase("")){
            ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1(mSHV);
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1("");
            con.setOperator("eq");
            cons.add(con);	
        }
        
        tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
        if(tblQLDTTXAHocViens != null && tblQLDTTXAHocViens.size() > 0)
        	tblQLDTTXAHocVien = tblQLDTTXAHocViens.get(0);
        
        //LopHoc cua HocVien
        cons.clear();
        if(tblQLDTTXAHocVien != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(tblQLDTTXAHocVien.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(new Long(0));
            con.setOperator("eq");
            cons.add(con);	
        }
        if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1(hocKyId);
            con.setOperator("eq");
            cons.add(con);	
        }
        
        {
        	ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);
        }
        tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
          
        cons.clear();
        if(tblQLDTTXAHocVien != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(tblQLDTTXAHocVien.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(new Long(0));
            con.setOperator("eq");
            cons.add(con);	
        }
        soTinChiTL = 0;
        tongSoTinChi = 0;
        diemTBTL = 0.0;
        Double tongDiem = 0.0;
        int soTinChiTinhDiem = 0;
        List<TblQLDTTXAHocVienChuongTrinhHoc> tblQLDTTXAHocVienChuongTrinhHocs = tblQLDTTXAHocVienChuongTrinhHocService.findtblQLDTTXAHocVienChuongTrinhHocByCondition(cons, 0, 0);
        for(TblQLDTTXAHocVienChuongTrinhHoc tblQLDTTXAHocVienChuongTrinhHoc : tblQLDTTXAHocVienChuongTrinhHocs){
        	
        	tongSoTinChi += tblQLDTTXAHocVienChuongTrinhHoc.getMonHocId().getSoTinChi();
        	
        	if(tblQLDTTXAHocVienChuongTrinhHoc.isHoanThanh() && tblQLDTTXAHocVienChuongTrinhHoc.getDiem() != null){
        		soTinChiTL += tblQLDTTXAHocVienChuongTrinhHoc.getMonHocId().getSoTinChi();
        		if (tblQLDTTXAHocVienChuongTrinhHoc.getDiem().compareTo(new Double(12)) != 0)
        		{        			
        			soTinChiTinhDiem += tblQLDTTXAHocVienChuongTrinhHoc.getMonHocId().getSoTinChi();
        			tongDiem += tblQLDTTXAHocVienChuongTrinhHoc.getDiem()* tblQLDTTXAHocVienChuongTrinhHoc.getMonHocId().getSoTinChi();
        		}
        	}
        }
        
        if (soTinChiTinhDiem != 0){
        	diemTBTL = tongDiem/soTinChiTinhDiem;
        }

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_ThoiKhoaBieuChinhThuc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
}

public void setTblQLDTTXAHocKyTrongNam(
		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
}

public Long getHocKyId() {
	return hocKyId;
}

public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}



public List<ThoiKhoaBieuPhongNgay> getThoiKhoaBieuPhongNgays() {
	return thoiKhoaBieuPhongNgays;
}

public void setThoiKhoaBieuPhongNgays(
		List<ThoiKhoaBieuPhongNgay> thoiKhoaBieuPhongNgays) {
	this.thoiKhoaBieuPhongNgays = thoiKhoaBieuPhongNgays;
}


public int getView() {
	return view;
}

public TblQLDTTXAHocVien getTblQLDTTXAHocVien() {
	return tblQLDTTXAHocVien;
}

public void setTblQLDTTXAHocVien(TblQLDTTXAHocVien tblQLDTTXAHocVien) {
	this.tblQLDTTXAHocVien = tblQLDTTXAHocVien;
}

public String getMSHV() {
	return mSHV;
}

public void setMSHV(String mshv) {
	mSHV = mshv;
}

public List<TblQLDTTXALopMonHocHocVien> getTblQLDTTXALopMonHocHocViens() {
	return tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXALopMonHocHocViens(
		List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens) {
	this.tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXAThoiKhoaBieuChinhThucs(
		Collection<TblQLDTTXAThoiKhoaBieuChinhThuc> tblQLDTTXAThoiKhoaBieuChinhThucs) {
	this.tblQLDTTXAThoiKhoaBieuChinhThucs = tblQLDTTXAThoiKhoaBieuChinhThucs;
}

public void setTblQLDTTXADMNgayTrongTuans(
		List<TblQLDTTXADMNgayTrongTuan> tblQLDTTXADMNgayTrongTuans) {
	this.tblQLDTTXADMNgayTrongTuans = tblQLDTTXADMNgayTrongTuans;
}

public void setTblQLDTTXAHocViens(List<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
	this.tblQLDTTXAHocViens = tblQLDTTXAHocViens;
}

public void setTblQLDTTXADMTietHocs(
		List<TblQLDTTXADMTietHoc> tblQLDTTXADMTietHocs) {
	this.tblQLDTTXADMTietHocs = tblQLDTTXADMTietHocs;
}

public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}

public void setTblQLDTTXALopMonHocs(
		List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs) {
	this.tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocs;
}

public void setView(int view) {
	this.view = view;
}

public Double getDiemTBTL() {
	return diemTBTL;
}

public void setDiemTBTL(Double diemTBTL) {
	this.diemTBTL = diemTBTL;
}

public int getSoTinChiTL() {
	return soTinChiTL;
}

public void setSoTinChiTL(int soTinChiTL) {
	this.soTinChiTL = soTinChiTL;
}

public int getTongSoTinChi() {
	return tongSoTinChi;
}

public void setTongSoTinChi(int tongSoTinChi) {
	this.tongSoTinChi = tongSoTinChi;
} 
 } 
 
